//
//  RootViewController.m
//  YaHooFrame
//
//  Created by dg11185_zal on 14/11/24.
//  Copyright (c) 2014年 dg11185. All rights reserved.
//

#import "RootViewController.h"
#import "MainView.h"
#import "LeftSideView.h"
#import "AddCityViewController.h"

#define SIDEWIDTH 0.15*self.view.frame.size.width

@interface RootViewController ()<MainViewDelegate,LeftSideViewDelegate,AddCityViewControllerDelegate>
{
    MainView *mainView;//主视图
    LeftSideView *leftView;//左侧视图
    BOOL isShowSide;//是否显示左侧视图
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    isShowSide = NO;
    //显示主视图
    [self showMainView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//主视图
-(void) showMainView{
    //城市数据
    NSArray *cityArray = [NSArray arrayWithObjects:@"广州市",@"东莞市", nil];
    //添加主视图
    mainView = [[MainView alloc] initWithFrame:self.view.bounds withArray:cityArray];
    mainView.delegate = self;
    [self.view addSubview:mainView];
    
    //给主视图添加滑动手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeSideView:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    mainView.userInteractionEnabled = YES;
    [mainView addGestureRecognizer:swipeGesture];
    
    //添加左侧视图
    leftView = [[LeftSideView alloc] initWithFrame:CGRectMake(-self.view.frame.size.width+SIDEWIDTH, 0, self.view.frame.size.width-SIDEWIDTH, self.view.frame.size.height) withArray:cityArray];
    leftView.delegate = self;
    [self.view addSubview:leftView];
}

#pragma mark -Other
//手势切换
-(void) swipeSideView:(UISwipeGestureRecognizer*)sender{
    NSLog(@"swipe");
    if (isShowSide) {
        //隐藏左侧图
        [UIView animateWithDuration:0.5 animations:^{
            leftView.frame = CGRectMake(-self.view.frame.size.width+SIDEWIDTH, 0, leftView.frame.size.width, leftView.frame.size.height);
            mainView.frame = CGRectMake(0, 0, mainView.frame.size.width, mainView.frame.size.height);
        }];
        isShowSide = !isShowSide;
        [mainView changeScroll];
    }
}

#pragma mark -Delegate

/**********MainViewDelegate************/
//点击切换按钮
-(void) showSideView{
    if (isShowSide) {
        //隐藏左侧图
        [UIView animateWithDuration:0.5 animations:^{
            leftView.frame = CGRectMake(-self.view.frame.size.width+SIDEWIDTH, 0, leftView.frame.size.width, leftView.frame.size.height);
            mainView.frame = CGRectMake(0, 0, mainView.frame.size.width, mainView.frame.size.height);
        }];
    }else{
        //显示左侧图
        [UIView animateWithDuration:0.5 animations:^{
            leftView.frame = CGRectMake(0, 0, leftView.frame.size.width, leftView.frame.size.height);
            mainView.frame = CGRectMake(leftView.frame.size.width, 0, mainView.frame.size.width, mainView.frame.size.height);
        }];
    }
    isShowSide = !isShowSide;
    [mainView changeScroll];
}

//添加新城市
-(void) addNewView{
    AddCityViewController *addVC = [[AddCityViewController alloc] init];
    addVC.delegate = self;
    [self presentViewController:addVC animated:YES completion:nil];
}

/**********LeftSideViewDelegate************/
//点击某一行
-(void) selectCell:(NSInteger)cityIndex{
    [mainView changeView:cityIndex];
    [self showSideView];
}

/**********AddCityViewControllerDelegate************/
//添加新城市成功
-(void) addCitySuccess:(NSString *)cityName{
    [mainView addNewCityView:cityName];
    [leftView addCity:cityName];
}


@end
